%To write the source current files 
% for cable computation
%latest date 29.9.5
% I forgot to compute the fields for the mean flow !
% Here we compute the field for mean (first value from 1D fft) 23.12.2005 !
% CAUTION DO NOT USE THIS FOR ANY OTHER COMPUTATION (USE WR_TSU.M)
MyOutPath = 'c:\Manoj\ocean\Source\180x360\tsunami\';
load c:\manoj\projects\tsunami\ts_iu_spec_old.mat IU_spec;
load c:\manoj\projects\tsunami\ts_iv_spec_old.mat IV_spec;
load c:\manoj\projects\tsunami\Bz;
Bz = rot90(Bz);
data_ur = zeros([180,360]);
data_ui = zeros([180,360]);
data_vr = zeros([180,360]);
data_vi = zeros([180,360]);
prd = 1./(fftfrq(600,1/(60/(24*3600))));

if isdir(MyOutPath) == 0,
    mkdir('c:\Manoj\ocean\','Source\180x360\tsunami\');
end;
%fp = fopen('c:\manoj\temp\a.project','wt');
for i = 1:1,
    IU1 = squeeze(IU_spec(i,:,:));
    IUr = rot90(real(IU1));
    IUr = IUr(1:2:end,2:2:end);
    data_ur(61:120,31:120) = IUr;
    
    IUi = rot90(imag(IU1));
    IUi = IUi(1:2:end,2:2:end);
    data_ui(61:120,31:120) = IUi;
    
    IV1 = squeeze(IV_spec(i,:,:));
    IVr = rot90(real(IV1));
    IVr = IVr(1:2:end,2:2:end);
    data_vr(61:120,31:120) = IVr;
    
    IVi = rot90(imag(IV1));
    IVi = IVi(1:2:end,2:2:end);
    data_vi(61:120,31:120) = IVi;
    
CUr = -data_vr.*Bz.*1e-9.*3.2;
CUi = -data_vi.*Bz.*1e-9.*3.2;
CVr = -data_ur.*Bz.*1e-9.*3.2;
CVi = -data_ui.*Bz.*1e-9.*3.2;

%CU = CU*1000; % Compression for e format 20 oct 2005
%CV = CV*1000; % This multiplication is compensated by scale factor in source file
%fprintf(fp,'%s\n',['    ' sprintf('%10.5f',prd(i)) '     ' sprintf('tsu%d',i) ' 1x1_mantle_k3.model ' sprintf('tsu%d',i) '.source' ' 1']);
fid = fopen([MyOutPath sprintf('tsu0') '.source'],'wt');
fid1 = fopen('c:\manoj\ocean\x3dg\header.txt','rt');
%the following format for alexei's x3dg is tested ok 20 oct 05
           for ii = 1:34,
                s=fgetl(fid1);
                fprintf(fid,'%s\n',s);
            end;
            s=fgetl(fid1);
for in = 1:180,
    for jn = 1:360,
       fprintf(fid,'%12.4e\n',CUr(in,jn));
   end;
end;
            fprintf(fid,'\n');
            for ii = 1:17,
            s=fgetl(fid1);
            fprintf(fid,'%s\n',s);
            end;
            s=fgetl(fid1);
for in = 1:180,
    for jn = 1:360,
        fprintf(fid,'%12.4e\n',CUi(in,jn));
    end;
end;
            fprintf(fid,'\n');
            for ii = 1:17,
            s=fgetl(fid1);
            fprintf(fid,'%s\n',s);
            end;
            s=fgetl(fid1);
 for in = 1:180,
    for jn = 1:360,       
        fprintf(fid,'%12.4e\n',CVr(in,jn));
    end;
end;
            fprintf(fid,'\n');
            for ii = 1:17,
            s=fgetl(fid1);
            fprintf(fid,'%s\n',s);
            end;
            s=fgetl(fid1);
  
 for in = 1:180,
    for jn = 1:360,       
        fprintf(fid,'%12.4e\n',CVi(in,jn));
    end;
end;
            fprintf(fid,'\n');
            for ii = 1:37,
            s=fgetl(fid1);
            fprintf(fid,'%s\n',s);
            end;
            s=fgetl(fid1);

           fclose(fid);
           fclose(fid1);
end;
fclose all;